const path = require('path');
const htmlPlugin = require('html-webpack-plugin')
const { CleanPlugin, DefinePlugin } = require('webpack');
console.log("-----NODE_ENV-----:" + process.env.NODE_ENV)
const mode=process.env.NODE_ENV === 'dev' ? 'development' : 'production';

const dataObj = require("./.env/common.json")
var SELF_ENV = {};
Object.keys(dataObj).forEach(envKey => {
    SELF_ENV[envKey] = dataObj[envKey]
})


const devCommonConfig = {}
if (mode === 'development') {
    devCommonConfig.devServer = {
        static: {
            directory: path.join(__dirname, 'static')
        },
        port: 9000,
        allowedHosts: ['all']
    }
}
module.exports = {
    entry: {
        'ourintegral': path.resolve(__dirname, 'src/index.tsx'),
    },
    devtool: mode === 'development' ? 'eval-cheap-module-source-map' : false,
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js'
    },
    resolve: {
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
        alias: {
            '~': path.resolve(__dirname, 'src'),
            'gitee-simple-db': 'gitee-simple-db/src/index.js'
        }
    },
    module: {
        rules: [
            { 
                test: /\.(js|jsx|ts|tsx)$/, 
                exclude: /node_modules/, 
                loader: "babel-loader",
                options: { // '@babel/preset-env', 
                    presets: [['@babel/preset-react', {"runtime": "automatic"}], '@babel/typescript'],
                    plugins: ['@babel/plugin-proposal-class-properties']
                }
            },
            {
                test: /\.(css|scss)$/,
                exclude: /node_modules/,
                use: [
                    'style-loader',
                    'css-loader',
                    'sass-loader'
                ]
            }
        ]
    },
    plugins: [
        new htmlPlugin({
            template: path.resolve(__dirname, 'index.html'),
            // inject: false,
            minify: false,
            hash: true
        }),
        new CleanPlugin(),
        new DefinePlugin({
            ACCESS_TOKEN: JSON.stringify(SELF_ENV.ACCESS_TOKEN),
            NICK_NAME: JSON.stringify(SELF_ENV.NICK_NAME),
        })

    ],
    mode: mode,
    devServer: devCommonConfig.devServer
}